package org.cycuchy.hp.components {
	
	import com.flexdotorg.controls.LoadingIcon;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.core.Application;
	import mx.core.BitmapAsset;
	
	import org.cycuchy.hp.app.HPApplication;
	import org.cycuchy.hp.data.FotoData;
	import org.cycuchy.hp.event.PreloadingImageEvent;
	
	public class FotoCanvas extends Canvas {
        
        private var _fotoData:FotoData;
        private var _scaleX:Number;
        private var _scaleY:Number;
		private var image:Image;
//		private var cursorHolder:UIComponent;
		private var li:LoadingIcon;
		private var b:Canvas;
		
        [Embed(source='assets/embed/img/polaroid.gif')]
		private static const polaroidImageClass:Class;
		
//		private static var busyCursorClass:Class = StyleManager.getStyleDeclaration("CursorManager").getStyle("busyCursor");
		
		public function FotoCanvas(scaleX:Number, scaleY:Number, polaroidWidth:uint, polaroidHeight:uint/*, fotoData:FotoData*/) {
			super();
			width = polaroidWidth;
			height = polaroidHeight;
//			_fotoData = fotoData;
			_scaleX = scaleX;
			_scaleY = scaleY;
			addEventListener(PreloadingImageEvent.LOAD, onLoad);
		}
		
		override protected function createChildren():void {
			super.createChildren();
			addBackground();
			addImage();
			addPolaroid();
//			addCursor();
		}		
		
		private function addBackground():void {
			var black:Canvas = new Canvas();
			black.width = 100;
			black.height = 110;
			black.setStyle("backgroundColor", 0x000000);
        	black.setStyle("horizontalCenter", 0);
        	black.setStyle("verticalCenter", -10);
			addChild(black);
		}
		
		private function addImage():void {
			image = new Image();
        	image.setStyle("horizontalCenter", 0);
        	image.setStyle("verticalCenter", -10);
			image.scaleX = _scaleX;
			image.scaleY = _scaleY;
			image.addEventListener(Event.COMPLETE, onComplete);
			addChild(image);
		}
		
		private function addCursor():void {
//        	var currentCursor:DisplayObject = new busyCursorClass;
//        	cursorHolder = new UIComponent();
//        	cursorHolder.setStyle("horizontalCenter", 0);
//        	cursorHolder.setStyle("verticalCenter", 0);
//        	cursorHolder.addChild(currentCursor);
//        	cursorHolder.addChild(new com.flexdotorg.controls.LoadingIcon());
//        	addChild(cursorHolder);
			if (li == null) {
				li = new LoadingIcon();
				li.horizontalCenter = 0;
				li.verticalCenter = 0;
	        	//li.setStyle("horizontalCenter", 0);
	        	//li.setStyle("verticalCenter", 0);
				addChild(li);
			}
		}
		
		private function addPolaroid():void {
			var bitmapAsset:BitmapAsset = BitmapAsset(new polaroidImageClass());
			var polaroid:Image = new Image();
			polaroid.source = bitmapAsset;
			addChild(polaroid);
		}
		
        private function onLoad(event:Event):void	{
//        	trace("FotoCanvas:onLoad ", event);
        	if (_fotoData != null) {
	        	addCursor();
    	    	image.load(this._fotoData.imgSrc);
        	}
        }
        
        private function onComplete(event:Event):void {
//        	trace("FotoCanvas::onComplete");
//        	image.filters = createFilters();
        	(image.content as Bitmap).smoothing = true;
//        	removeChild(cursorHolder);
//        	cursorHolder = null;
			removeChild(li);
			li = null;
        	addButtonCanvas();
        }
        
        private function addButtonCanvas():void {
			b = new Canvas();
			b.width = 98;
			b.height = 102;
			b.setStyle("backgroundColor", 0xFFFFFF);
        	b.setStyle("horizontalCenter", -1);
        	b.setStyle("verticalCenter", -9);
			b.useHandCursor = true;
			b.buttonMode = true;
			b.alpha = 0.1;
			b.addEventListener(MouseEvent.CLICK, onClick);			
			addChild(b);
        }

        private function onClick(event:Event):void {
//        	trace("FotoCanvas::onClick");
        	(Application.application as HPApplication).showTitleWindow(this.image.content, this._fotoData.id);
        }
        
        public function set fotoData(value:FotoData):void {
        	_fotoData = value;
        }
        
        public function clean():void {
        	if (li != null) {
				removeChild(li);
				li = null;
        	}
        	if (b != null) {
        		removeChild(b);
        		b = null;
        	}
        	if (_fotoData != null) {
        		_fotoData = null;
        	}
        	image.source = null;
        }
        
        
        //http://merhl.com/flex2_samples/filterExplorer/
//		private function createFilters():Array {
//			var filters:Array = new Array();
//		    var dFilter1:GlowFilter = new GlowFilter();
//		    //var dFilter2:DropShadowFilter = new DropShadowFilter();
//		    
//		    dFilter1.alpha = 1;
////		    dFilter1.angle = 45;
////		    dFilter1.distance = 2;
//		    dFilter1.blurX = 15;
//		    dFilter1.blurY = 15;
//		    dFilter1.strength = 255;
//		    dFilter1.inner = true;
//		    dFilter1.color = 0x0898BE;
//		    filters.push(dFilter1);
//
////		    dFilter2.alpha = 0.4;
////		    dFilter2.angle = 45;
////		    dFilter2.distance = 2;
////		    dFilter1.blurX = 10;
////		    dFilter1.blurY = 10;
////		    dFilter2.strength = 2;
////		    dFilter2.inner = false;
////		    filters.push(dFilter2);
//		    
//		    return filters; 
//		}
        
	}
	
}